//
//  SpecialTableViewCell.m
//  demo
//
//  Created by dllo on 15/7/13.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "SpecialTableViewCell.h"
#import "UIImageView+WebCache.h"
@implementation SpecialTableViewCell
-(void)dealloc
{
    [_view release];
    [_img release];
    [_name release];
    [_titleLabel release];
    [_back  release];
    [super dealloc];
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.view = [[UIView alloc]init];
        self.view.clipsToBounds = YES;
        [self.contentView addSubview:self.view];
        [_view release];
        
        self.img = [[UIImageView alloc]init];
        [self.view addSubview:self.img];
//        [self.img.layer setCornerRadius:6.0];
//        self.img.layer.masksToBounds = YES;
        [_img release];
        
        self.back = [[UIImageView alloc]init];
        [self.view addSubview: self.back];
        [_back release];
        
        self.name = [[UILabel alloc]init];
        [self.back addSubview:self.name];
        self.name.textColor = [UIColor whiteColor];
        [self.name setFont:[UIFont fontWithName:@"Helvetica-Bold" size:20]];
        self.name.backgroundColor = [UIColor clearColor];
        [_name release];
        
        self.titleLabel = [[UILabel alloc]init];
        [self.back addSubview:self.titleLabel];
        self.titleLabel.textColor = [UIColor whiteColor];
        [self.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:15]];
        self.titleLabel.backgroundColor = [UIColor clearColor];
        [_titleLabel release];
        [self setupImageWithTitle];
    }
    return  self;
}
- (void)setupImageWithTitle
{
    self.view.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, ([UIScreen mainScreen].bounds.size.height - 49 - 64 - 40 - 10) / 2.5);
    self.img.frame = CGRectMake(10, -50, self.view.frame.size.width - 20, self.view.frame.size.height + 30);
    
    self.back.frame = CGRectMake(10, self.view.frame.origin.y + self.view.frame.size.height - 65, self.view.bounds.size.width - 20, 65);
    
    self.name.frame = CGRectMake(10, 10, self.back.bounds.size.width - 20, 25);
    
    self.titleLabel.frame =CGRectMake(self.name.frame.origin.x, self.name.frame.origin.y + self.name.frame.size.height + 2, self.img.bounds.size.width - 20, 25);
}
//-(void)layoutSubviews
//{
//    [super layoutSubviews];
//    self.view.frame = CGRectMake(self.contentView.frame.origin.x + 5, self.contentView.frame.origin.y
//    +5, self.contentView.frame.size.width - 20, self.contentView.frame.size.height - 20);
//    
//    self.img.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);
//    
//    self.back.frame = CGRectMake(self.img.frame.origin.x - 5, self.img.frame.origin.y + 150, self.img.bounds.size.width, 80);
//    
//    self.name.frame = CGRectMake(5, 0, self.img.bounds.size.width - 20, 35);
//    
//    self.titleLabel.frame =CGRectMake(self.name.frame.origin.x, self.name.frame.origin.y + self.name.frame.size.height, self.img.bounds.size.width - 20, 25);
//    
//}


-(void)setSpecial:(special *)special
{
    if (_special != special) {
        [_special release];
        _special = [special retain];
    }
    
    [self.img setImageWithURL:[NSURL URLWithString:special.image_url] placeholderImage:[UIImage imageNamed:@"place_holder.jpg"]];
    self.name.text = special.name;
    self.titleLabel.text =special.title;
    
}
+ (instancetype)tableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"tableViewCell";
    SpecialTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[SpecialTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    return cell;
}

- (void)cellOnTableView:(UITableView *)tableView didScrollOnView:(UIView *)view
{
    CGRect rectInSuperview = [tableView convertRect:self.frame toView:view];
    float distanceFromCenter = CGRectGetHeight(view.frame)/2 - CGRectGetMinY(rectInSuperview);
    float difference = CGRectGetHeight(self.img.frame) - CGRectGetHeight(self.frame);
    float move = (distanceFromCenter / CGRectGetHeight(view.frame)) * difference;
    CGRect imageRect = self.img.frame;
    imageRect.origin.y = -(difference/.5)+move;
    self.img.frame = imageRect;
}
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
